function AMAgetCellStatus(btnCell, btnText)
{

    // mark active with !
    if(btnCell.tagName == 'B') {return '!' + btnText}

    // mark disabled with -
    // mark available with nothing
    if(btnCell.tagName == 'FORM') {
    if(btnCell.childNodes[2].getAttribute('class') == "disabled") {return '-' + btnText} else {return btnText}
    } else {
        if(btnCell.tagName == 'INPUT') {
            if(btnCell.getAttribute('class') == "disabled") {return '-' + btnText} else {return btnText}
        }
    }
}

var enableOCDC = (localStorage.getItem ("po_ama_enable_ocdc")=="true")?true:false;			// buttons for OC, BAL and DC
var enableTimebomb = (localStorage.getItem ("po_ama_enable_tb")=="true")?true:false;		// buttons for TB1 and TB2
var enableBoostsAgility = (localStorage.getItem ("po_ama_enable_ab")=="true")?true:false;		// agility boost button
var enableBoostsTaming = (localStorage.getItem ("po_ama_enable_tab")=="true")?true:false;		// taming boost button
var enableBoostsCloaking = (localStorage.getItem ("po_ama_enable_cb")=="true")?true:false;	// cloaking boost button
var useBtnForActivated = (localStorage.getItem ("po_ama_enable_show_active_button")=="true")?true:false;		// makes buttons for activated options
var showDisabledButtons = (localStorage.getItem ("po_ama_enable_show_hidden_buttons")=="true")?true:false;	// makes buttons for unavailable options
var enableBoosts = enableBoostsAgility || enableBoostsTaming || enableBoostsCloaking;

var combatModes = '';
var boostModes = '';
var timebombModes = '';
var tblActivations;

function AMAcheckActivationButtons() {

    // check if called from hidden frame then find buttons and update
    if(window.frameElement && window.frameElement.id == 'hidefrm') {
    
        var mainframe = window.parent.frames[2].document;
            
        if(enableOCDC) {
            var divActivations = mainframe.getElementById('activations');
            divActivations.innerHTML = '';
            AMAaddButtonsOCDC(divActivations)
        }

        if(enableTimebomb) {
            var divTimebombs = mainframe.getElementById('timebombs');
            divTimebombs.innerHTML = '';
            AMAaddButtonsTimebomb(divTimebombs)
        }

        if(enableBoosts) {
            var divBoosts = mainframe.getElementById('boosts');
            divBoosts.innerHTML = '';
            AMAaddButtonsBoosts(divBoosts)
        }

    }
}

function AMAaddBtn(node, cmd, actName, val, label, config, clr) {

    var idx = config.search(val);

    // selected
    if(config[idx-1] == '!') {
        if(useBtnForActivated)
            {node.innerHTML = node.innerHTML + " <input type='submit' value='" + label + "' style='background-color:" + clr + "font-size:8pt;font-weight:bold;padding:1px;'>"}
        else
            {node.innerHTML = node.innerHTML + " <font style='color:" + clr + "font-size:8pt;font-weight:bold;padding:1px;'>" + label + "</font>"}
    }

    // disabled
    if(showDisabledButtons && config[idx-1] == '-') {
        node.innerHTML = node.innerHTML + " <input type='submit' value='" + label + "' disabled class='disabled' style='font-size:8pt;font-weight:bold;padding:1px;'>";
    }

    // clickable
    if(config[idx-1] == '|') {
        node.innerHTML = node.innerHTML + " <form action='overview_advanced_skills.php' Target='hidefrm' Method='post' style='display:inline'><input type='hidden' name='action' value='" + cmd + "'><input type='hidden' name='" + actName + "' value='" + val + "'><input type='submit' value='" + label + "' style='color:" + clr + "font-size:8pt;font-weight:bold;padding:1px;'></form>";
    }

}

function AMAaddButtonsTimebomb(aDiv) {

    // load timebombs, default is none available
    var timeBombs = localStorage.getItem('ama_timeBombs');
    if(!timeBombs) timeBombs = '';

    if(timeBombs != '') {
        AMAaddBtn(aDiv, 'deploy_timebomb', 'timebomb_type', 'type_1', 'Stun bomb', timeBombs, '#F0F000;');
        AMAaddBtn(aDiv, 'deploy_timebomb', 'timebomb_type', 'type_2', 'AP bomb', timeBombs, '#F0F000;');	
    }
}

function AMAaddButtonsOCDC(aDiv) {

    // load combat modes, default is balanced active and others disabled
    var combatModes = localStorage.getItem('ama_combatModes');
    if(!combatModes) combatModes = '|-offensive|!balanced|-defensive';

    // add combat mode buttons
    AMAaddBtn(aDiv, 'switch_combat_mode', 'combat_mode', 'offensive', 'OC', combatModes, '#FF0000;');
    AMAaddBtn(aDiv, 'switch_combat_mode', 'combat_mode', 'balanced', 'BAL', combatModes, '#A0A0A0;');
    AMAaddBtn(aDiv, 'switch_combat_mode', 'combat_mode', 'defensive', 'DC', combatModes, '#00FF00;');

}

function AMAaddButtonsBoosts(aDiv) {

    // load boost modes, default is no boosts available
    var boostModes = localStorage.getItem('ama_boostModes');
    if(!boostModes) boostModes = '';

    // these only get added if boosts available
    if(enableBoostsAgility) AMAaddBtn(aDiv, 'boost', 'boost', 'agility', 'AB', boostModes, '#00F0F0;');
    if(enableBoostsTaming) AMAaddBtn(aDiv, 'boost', 'boost', 'taming', 'TA', boostModes, '#00F0F0;');
    if(enableBoostsCloaking) AMAaddBtn(aDiv, 'boost', 'boost', 'cloaking', 'CL', boostModes, '#00F0F0;');

}

// find ze table
var headers = document.getElementsByTagName('h2');
for(var i = 0; i < headers.length; i++)	{
    if(headers[i].innerHTML == 'Activations') tblActivations = headers[i].nextSibling.nextSibling;
}

// do for each table row
for(var i = 0; i < tblActivations.rows.length; i++)
{
    if(tblActivations.rows[i].cells.length == 5) {

        // check combat modes
        if(tblActivations.rows[i].cells[1].innerHTML == 'Combat Mode') {
            combatModes = combatModes + '|' + AMAgetCellStatus(tblActivations.rows[i].cells[2].firstChild, 'offensive');
            combatModes = combatModes + '|' + AMAgetCellStatus(tblActivations.rows[i].cells[3].firstChild, 'balanced');
            combatModes = combatModes + '|' + AMAgetCellStatus(tblActivations.rows[i].cells[4].firstChild, 'defensive');		
        }

        // check boosts
        if(tblActivations.rows[i].cells[1].innerHTML == 'Boosts') {
            boostModes = boostModes + '|' + AMAgetCellStatus(tblActivations.rows[i].cells[2].firstChild, 'agility');
            boostModes = boostModes + '|' + AMAgetCellStatus(tblActivations.rows[i].cells[3].firstChild, 'taming');
            boostModes = boostModes + '|' + AMAgetCellStatus(tblActivations.rows[i].cells[4].firstChild, 'cloaking');		
        }
    }

    if(tblActivations.rows[i].cells.length == 4) {

        // check timebombs
        if(tblActivations.rows[i].firstChild.tagName == 'TD' && tblActivations.rows[i].childNodes[1].innerHTML == "Timebomb<br>Management") {
            timebombModes = timebombModes + '|' + AMAgetCellStatus(tblActivations.rows[i].childNodes[2].firstChild, 'type_1');
            timebombModes = timebombModes + '|' + AMAgetCellStatus(tblActivations.rows[i].childNodes[2].childNodes[3], 'type_2');
        }
    }

}

if(tblActivations != null) {

    // store all
    localStorage.setItem('ama_combatModes', combatModes);
    localStorage.setItem('ama_boostModes', boostModes);
    localStorage.setItem('ama_timeBombs', timebombModes);
    //alert("combatModes: " + combatModes);
    //alert("boostModes: " + boostModes);
    //alert("timebombModes: " + timebombModes);

    // update btns (in case called from hidden frame)
    AMAcheckActivationButtons();

}